<template>
  <div class="updateName">
    <!-- 导航栏 -->
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="确定"
      @click-left="$emit('close')"
      @click-right='onConfirm'
    />
    <!-- /导航栏 -->
    <van-field
      v-model="localName"
      rows="2"
      autosize
      type="textarea"
      maxlength="7"
      placeholder="请输入新昵称"
      show-word-limit
      class="field-wrap"
    />
  </div>
</template>

<script>
import { updateUserName } from '@/api/user'
export default {
  name: 'UpdateName',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  computed: {},
  watch: {},
  created () { },
  methods: {
    async onConfirm () {
      try {
        const { data } = await updateUserName({
          name: this.localName
        })
        console.log(data)
        // 关闭弹层
        this.$emit('close')
        // 展示视图
        this.$emit('input', this.localName)
        // 提示成功
        this.$toast('昵称更新成功')
      } catch (err) {
        this.$toast.fail('昵称更新失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.field-wrap {
  padding: 20px;
}
</style>
